<!DOCTYPE html>
<html lang="en">

<head>
	<meta charset="UTF-8">
	<meta name="viewport" content="width=device-width, initial-scale=1.0">
	<meta http-equiv="X-UA-Compatible" content="ie=edge">
	<script src="static/assets/global/plugins/jquery-1.11.0.min.js"></script>
	<link href="static/assets/global/plugins/bootstrap/css/bootstrap.min.css" rel="stylesheet" type="text/css"/>
	<link href="static/assets/global/css/components.css" rel="stylesheet" type="text/css"/>
	<link href="static/assets/admin/pages/css/login.css" rel="stylesheet" type="text/css"/>
	<title>Document</title>
</head>

<body class="login">
<div class="content">
	<h3 class="form-title">商品详情</h3>
	<div class="form-group">
		<label class="control-label">商品名称</label>
		<div>
			<label class="control-label" id="title"/>
		</div>

	</div>
	<div class="form-group">
		<label class="control-label">价格</label>
		<div>
			<label class="control-label" id="price"/>
		</div>
	</div>
	<div class="form-group">
		<label class="control-label">库存</label>
		<div>
			<label class="control-label" id="stock"/>
		</div>
	</div>
	<div class="form-group">
		<label class="control-label">描述</label>
		<div>
			<label class="control-label" id="description"/>
		</div>

	</div>
	<div class="form-group">
		<label class="control-label">销量</label>
		<div>
			<label class="control-label" id="sales"/>
		</div>

	</div>
	<div class="form-group">
		<label class="control-label">商品图片</label>
		<div>
			<img style="width:200px;height:auto;" name="imgUrl" id="imgUrl"/>
		</div>
	</div>
	<div class="form-actions">
		<button id="order" class="btn blue" type="button">提交订单</button>
	</div>
</div>
</body>
<script>
    function getParam(variable) {
        var query = window.location.search.substring(1);
        var vars = query.split("&");
        for (var i = 0; i < vars.length; i++) {
            var pair = vars[i].split("=");
            if (pair[0] === variable) {
                return pair[1];
            }
        }
        return (false);
    }

    var g_itemVO = {};
    $("document").ready(function () {
        $("#order").on("click", function () {
            $.ajax({
                type: 'POST',
                contentType: "application/x-www-form-urlencoded",
                url: "http://localhost:8090/longer/order/create",
                data: {
                    "itemId": g_itemVO.id,
                    //写死的
                    "account": 1
                },
                //允许跨域授信请求
                xhrFields: {withCredentials: true},
                success: function (data) {
                    if (data.status === "success") {
                        alert("下单成功");
                        window.location.reload();
                    } else {
                        alert("下单失败" + data.data.errorMsg);
                        if (data.data.errorCode === 10003) {
                            window.location.href = "login.html";
                        }
                    }
                },
                error: function (data) {
                    alert("下单失败" + data.responseText)
                }
            });
        });
        //获取商品详情
        $.ajax({
            type: 'GET',
            contentType: "application/x-www-form-urlencoded",
            url: "http://localhost:8090/longer/item/id",
            data: {
                "id": getParam("id")
            },
            //允许跨域授信请求
            xhrFields: {withCredentials: true},
            success: function (data) {
                if (data.status === "success") {
                    g_itemVO = data.data;
                    reloadDom();
                } else {
                    alert("获取信息失败" + data.data.errorMsg)
                }
            },
            error: function () {
                alert("获取信息失败" + data.responseText)
            }
        });
        //不可以冒泡往上传递
        return false;

    });

    function reloadDom() {
        $("#description").text(g_itemVO.description);
        $("#title").text(g_itemVO.title);
        $("#price").text(g_itemVO.price);
        $("#imgUrl").attr("src", g_itemVO.imgUrl);
        $("#stock").text(g_itemVO.stock);
        $("#sales").text(g_itemVO.sales);
    }

</script>

</html>